package com.yehaikeji.workflow.controller.management;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yehaikeji.workflow.bean.CountBean;
import com.yehaikeji.workflow.bean.Model;
import com.yehaikeji.workflow.bean.SmsOperation;
import com.yehaikeji.workflow.bean.incident;
import com.yehaikeji.workflow.bean.thing;
import com.yehaikeji.workflow.bean.unit;
import com.yehaikeji.workflow.bean.user;
import com.yehaikeji.workflow.common.assist.assist;
import com.yehaikeji.workflow.controller.bean.ResultMessage;
import com.yehaikeji.workflow.controller.bean.resultlist;
import com.yehaikeji.workflow.service.ModelService;
import com.yehaikeji.workflow.service.SmsOperationService;
import com.yehaikeji.workflow.service.incidentService;
import com.yehaikeji.workflow.service.thingService;
import com.yehaikeji.workflow.service.unitService;
import com.yehaikeji.workflow.service.userService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author ybt
 *
 */
@Controller
@RequestMapping("app")
public class AppController {
	@Autowired
	private userService userService;
	@Autowired
	private ModelService modelService;
	@Autowired
	private SmsOperationService smsOperationService;
	@Autowired
	private thingService thingService;
	@Autowired
	private incidentService incidentService;
	@Autowired
	unitService unitService;

	@RequestMapping(value = "/user/login", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户登陆,返回access_token", notes = "")
	private ResultMessage login(String userName, String password, HttpServletRequest request) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			} else {
				resultMessage.setData(userService.appLogin(userName, password));
				resultMessage.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/user/detail", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据accessToken获取用户信息", notes = "")
	private ResultMessage detail(@RequestParam String accessToken) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			resultMessage.setData(userService.selectAccessToken(accessToken));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/noteModel/add", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增短信模板", notes = "")
	private ResultMessage noteModelAdd(@RequestParam String accessToken, @RequestParam String name,
			@RequestParam String content) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			modelService.addModel(new Model(user.getUserid(), name, content));
			resultMessage.setSuccess(true);
			resultMessage.setMessage("新增模板成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/noteModel/modify", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "修改短信模板", notes = "")
	private ResultMessage noteModelModify(@RequestParam String accessToken, @RequestParam String id,
			@RequestParam String name, @RequestParam String content) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			Model model = new Model();
			model.setId(id);
			model.setName(name);
			model.setContent(content);
			modelService.modifyModel(model);
			resultMessage.setSuccess(true);
			resultMessage.setMessage("修改模板成功！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/noteModel/delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除短信模板", notes = "")
	private ResultMessage noteModelDelete(@RequestParam String accessToken, @RequestParam String id) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			int result = modelService.deleteModel(id);
			if (result > 0) {
				resultMessage.setMessage("删除数据成功");
				resultMessage.setSuccess(true);
			} else {
				resultMessage.setMessage("未找到删除的数据");
				resultMessage.setSuccess(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/noteModel/list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "短信模板列表", notes = "")
	private ResultMessage noteModelList(@RequestParam String accessToken, String name) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			List<Model> list = modelService.listPageModels(name);
			resultMessage.setData(list);
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/sms/send", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "发送短信列表", notes = "")
	private ResultMessage smsSend(@RequestParam String accessToken, @RequestParam String thingId,
			@RequestParam String phone, @RequestParam String modelId, @RequestParam String content) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			SmsOperation smsOperation = new SmsOperation();
			smsOperation.setModelId(modelId);
			smsOperation.setThingId(thingId);
			smsOperation.setPhone(phone);
			smsOperation.setUserId(user.getUserid());
			smsOperation.setType("1");
			smsOperation.setContent(content);
			resultMessage.setData(smsOperationService.addSmsOperation(smsOperation));
			resultMessage.setMessage("短信发送成功");
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/chat/detail", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查看某个事项的短信记录", notes = "")
	private ResultMessage chatDetail(@RequestParam String accessToken, @RequestParam String phone,
			@RequestParam String thingId) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			user user = userService.selectAccessToken(accessToken);
			if (null == user) {
				throw new RuntimeException("accessToken无效");
			}
			List<SmsOperation> list = smsOperationService.getChatDetailByPhone(phone, thingId);
			resultMessage.setData(list);
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "统计列表")
	public resultlist list(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam Integer typeid,String startDate, String endDate) {
		resultlist resultMessage = new resultlist();
		try {
			if (page <= 0 || limit <= 0) {
				resultMessage.setMsg("参数错误");
				resultMessage.setCode(1);
			} else if(typeid==null||typeid<=0||typeid>4){
				resultMessage.setMsg("typeid参数错误");
				resultMessage.setCode(1);
			}{
				PageHelper.startPage(page, limit);
				List<CountBean> countBeanList = thingService.selectCount(startDate, endDate,typeid);
				if(countBeanList.get(0)==null) {
					countBeanList.remove(0);
				}
				PageInfo<CountBean> thinginfor = new PageInfo<>(countBeanList);

				resultMessage.setCount(thinginfor.getTotal());
				resultMessage.setData(thinginfor.getList());
				resultMessage.setCode(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}

	// 项目下路径位置
	String uploademun = "/resource/upload";

	@RequestMapping(value = "morefileupload", method = RequestMethod.POST)
	@ApiOperation(value = "上传文件公共接口")
	@ResponseBody
	public ResultMessage fileuploads(@RequestParam(value = "file") MultipartFile file[], HttpServletRequest request) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			System.out.println("开始上传");
			if (file.length == 0) {
				resultMessage.setMessage("没有文件");
				resultMessage.setSuccess(false);
			} else {
				List<String> list = new ArrayList<>();
				for (MultipartFile multipartFile : file) {
					if (multipartFile.isEmpty()) {
						System.out.println("文件为空");
					} else {
						// 限制大小
						if (multipartFile.getSize() > 1024 * 1024 * 5) {
							System.out.println("超过大小");
						} else {
							// 创建网络资源路径
							String realurl = request.getScheme() + "://" + request.getServerName() + ":"
									+ request.getServerPort() + request.getContextPath() + uploademun;
							// 创建真实保存路径
							String realpath = request.getSession().getServletContext().getRealPath("") + uploademun;
							String name = multipartFile.getOriginalFilename();
							String newname = UUID.randomUUID().toString().replaceAll("-", "")
									+ name.substring(name.lastIndexOf("."));
							File file2 = new File(realpath + "/" + newname);
							multipartFile.transferTo(file2);
							list.add(realurl + "/" + newname);
						}
					}
				}
				resultMessage.setData(list);
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}

		return resultMessage;
	}

	@RequestMapping(value = "incidentthing", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "要签字操作接口", notes = "点击下一步提交时调用,根据不同的typeid结合当前的status判断所要输入的内容")
	public ResultMessage incidentthing(incident incident) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(incident.getThingid())) {
				throw new RuntimeException("thingid为空");
			}
//			if(StringUtil.isEmpty(incident.getAcceptname())){
//				throw new RuntimeException("acceptname为空");
//			}
			if(incident.getStatus() == null ){
				throw new RuntimeException("status为空");
			} 
			if(StringUtil.isEmpty(incident.getUserid())) {
				throw new RuntimeException("userid为空");
			}
			if(incident.getCreatedate() == null) { 
				throw new RuntimeException("createdate为空");
			}
			resultMessage.setData(incidentService.insertnextincident(incident));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "incidentthingok", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "直接确定操作接口", notes = "点击下一步提交时调用,根据不同的typeid结合当前的status判断所要输入的内容")
	public ResultMessage incidentthinga(incident incident) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(incident.getThingid())) {
				throw new RuntimeException("thingid为空");
			}
//			if(StringUtil.isEmpty(incident.getAcceptname())){
//				throw new RuntimeException("acceptname为空");
//			}
			if(incident.getStatus() == null ){
				throw new RuntimeException("status为空");
			} 
			if(StringUtil.isEmpty(incident.getUserid())) {
				throw new RuntimeException("userid为空");
			}
			if(incident.getCreatedate() == null) { 
				throw new RuntimeException("createdate为空");
			}
			resultMessage.setData(incidentService.insertnextincidenta(incident));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "incidentthingsignature", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "签名接口", notes = "点击签名完成时调用")
	public ResultMessage incidentthingsignature(String incidentid, @ApiParam(value = "交件者签名") String signatures,
			@ApiParam(value = "接件者签名") String signaturea) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(incidentid) || StringUtil.isEmpty(signaturea)) {
				throw new RuntimeException("参数为空");
			}
			incidentService.signature(incidentid, signatures, signaturea);
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	/**
	 * @param thingid
	 * @return
	 */
	@RequestMapping(value = "incidentselectstatus", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "可选状态")
	public ResultMessage incidentselectstatus(String thingid) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(thingid)) {
				throw new RuntimeException("参数为空");
			}
			resultMessage.setData(incidentService.incidentselectstatus(thingid));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "inserthing", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加一个事务", notes = "typeid=1是代办，typeid=2代发，typeid=3是代收,typeid=4一体化救助平台")
	public ResultMessage inserthingandincident(thing thing,
			@ApiParam(value = "交件时间") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date createdatei, String sendname,
			String acceptname, @ApiParam(value = "交件社区id") String unitida,
			@ApiParam(value = "中转部门（接件部门）id") String unitidb,@ApiParam("办理部门") String unitidc) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(thing.getUserid())) {
				throw new RuntimeException("userId不能为空");
			}
			if (thing.getTypeid() == null) {
				throw new RuntimeException("Typeid不能为空");
			}
			if (createdatei == null) {
				throw new RuntimeException("createdatei不能为空");
			}
//			if (StringUtil.isEmpty(sendname)) {
//				throw new RuntimeException("sendname不能为空");
//			}
			if (StringUtil.isEmpty(thing.getName())) {
				throw new RuntimeException("name不能为空");
			}
			if (thing.getNumber() == null) {
				throw new RuntimeException("number不能为空");
			}
//			if (StringUtil.isEmpty(acceptname)) {
//				throw new RuntimeException("acceptname不能为空");
//			}
			if (StringUtil.isEmpty(unitida)) {
				throw new RuntimeException("unitida不能为空");
			}
			if (StringUtil.isEmpty(unitidb)) {
				throw new RuntimeException("unitidb不能为空");
			}
			resultMessage.setData(thingService.inserthing(thing, createdatei, sendname, acceptname, unitida, unitidb,unitidc));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "inserthingok", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加一个事务,不签名", notes = "typeid=1是代办，typeid=2代发，typeid=3是代收,typeid=4一体化救助平台")
	public ResultMessage inserthingandincidentok(thing thing,
			@ApiParam(value = "交件时间") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date createdatei, String sendname,
			String acceptname, @ApiParam(value = "交件社区id") String unitida,
			@ApiParam(value = "中转部门（接件部门）id") String unitidb) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			System.out.println(thing);
			if (StringUtil.isEmpty(thing.getUserid())) {
				throw new RuntimeException("userId不能为空");
			}
			if (thing.getTypeid() == null) {
				throw new RuntimeException("Typeid不能为空");
			}
			if (createdatei == null) {
				throw new RuntimeException("createdatei不能为空");
			}
//			if (StringUtil.isEmpty(sendname)) {
//				throw new RuntimeException("sendname不能为空");
//			}
			if (StringUtil.isEmpty(thing.getName())) {
				throw new RuntimeException("name不能为空");
			}
			if (thing.getNumber() == null) {
				throw new RuntimeException("number不能为空");
			}
//			if (StringUtil.isEmpty(acceptname)) {
//				throw new RuntimeException("acceptname不能为空");
//			}
			if (StringUtil.isEmpty(unitida)) {
				throw new RuntimeException("unitida不能为空");
			}
			if (StringUtil.isEmpty(unitidb)) {
				throw new RuntimeException("unitidb不能为空");
			}
			resultMessage
					.setData(thingService.inserthingok(thing, createdatei, sendname, acceptname, unitida, unitidb));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "selectthingpagelist", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "事务列表")
	public resultlist selectthingpagelimit(@RequestParam Integer page, @RequestParam Integer limit, String bname,
			@ApiParam("按状态查找-1为办结，-2为未办结，status>0为单个状态查询")Integer status, Integer typeid, String bphone, String thingname,String startDate,String endDate,@ApiParam("发件社区/单位id(暂不支持)")String unitida,@ApiParam("收件/办理id(用于筛选新建的时候填写的办理部门的事项)")String unitidc) {
		resultlist resultMessage = new resultlist();
		try {
			if (page <= 0 || limit <= 0) {
				resultMessage.setMsg("参数错误");
				resultMessage.setCode(1);
			} else {
				PageHelper.startPage(page, limit);
				
				List<thing> things = thingService.selectlistpage(bname, status, typeid, bphone, thingname,startDate,endDate,unitida,unitidc);
				PageInfo<thing> thinginfor = new PageInfo<>(things);

				resultMessage.setCount(thinginfor.getTotal());
				resultMessage.setData(thinginfor.getList());
				resultMessage.setCode(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}

	@RequestMapping(value = "deletethingthis", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "删除一条事务")
	public ResultMessage deletething(String thingid) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(thingid)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			} else {
				thingService.deletethingthis(thingid);
				resultMessage.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "insertunit", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增加部门信息", notes = "type=1为社区，type=2办理部门或者中转部门")
	private ResultMessage insertunit(unit unit) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(unit.getName()) || unit.getType() == null || unit.getType() <= 0
					|| unit.getType() > 2) {
				resultMessage.setMessage("参数为空或type错误");
				resultMessage.setSuccess(false);
			} else {
				unit.setUnitid(unitService.insertunit(unit));
				resultMessage.setData(unit);
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return resultMessage;
	}

	@RequestMapping(value = "getunitlist", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "返回所有部门信息", notes = "type=1时表示社区部门，type=2时表示办事部门")
	private ResultMessage selectallunit(Integer type) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (type == null || type <= 0 || type > 2) {
				throw new RuntimeException("参数为空或错误");
			}
			resultMessage.setData(unitService.selectunitall(type));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return resultMessage;
	}

	@RequestMapping(value = "delectunit", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "删除一个部门信息")
	private ResultMessage deleteunit(String unitid) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			resultMessage.setData(unitService.deleteunit(unitid));
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setSuccess(false);
			resultMessage.setMessage(e.getMessage());
		}
		return resultMessage;
	}

	@RequestMapping(value = "/insertUser", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "新增用户", notes = "添加一个用户到数据库")
	private ResultMessage insertUser(String name, String password, Integer type) throws UnsupportedEncodingException {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(name) || type == null || StringUtil.isEmpty(password)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数不能为空");
			} else {
				String pattern = "[^\\u4e00-\\u9fa5]+";
				Pattern pat = Pattern.compile(pattern);
				Matcher matcher = pat.matcher(password);
				if (!matcher.matches()) {
					throw new RuntimeException("不能为汉字");
				} else {
					userService.insertSelective(name, password, type);
					resultMessage.setSuccess(true);
					resultMessage.setMessage("添加成功！");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/modificationpas", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "修改密码", notes = "")
	private ResultMessage modificationpas(@RequestParam String userName, @RequestParam String newpassword) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			String pattern = "[^\\u4e00-\\u9fa5]+";
			Pattern pat = Pattern.compile(pattern);
			Matcher matcher = pat.matcher(newpassword);
			if (!matcher.matches()) {
				throw new RuntimeException("不能为汉字");
			}
			userService.updateByuserNmaemodifipas(userName, "", newpassword);
			resultMessage.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/userlistpagelist", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户列表", notes = "分页按名称搜索，按类型分类")
	private resultlist userlistpagelist(@RequestParam Integer page, @RequestParam Integer limit,
			@RequestParam(required = false) String userName, @RequestParam(required = false) Integer type) {
		resultlist resultMessage = new resultlist();
		try {
			if (page == null || page <= 0 || limit == null || limit <= 0) {
				resultMessage.setCode(1);
				resultMessage.setMsg("参数错误");
			} else {
				PageHelper.startPage(page, limit);
				System.out.println(userName);
				List<user> list = userService.userlistpagelist(userName, type);

				PageInfo<user> pageInfo = new PageInfo<>(list);
				resultMessage.setData(pageInfo.getList());
				resultMessage.setCount(pageInfo.getTotal());
				resultMessage.setCode(0);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}

	@RequestMapping(value = "/userdelete", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "删除用户")
	private ResultMessage userdelete(String userid) {
		ResultMessage resultMessage = new ResultMessage();
		try {
			if (StringUtil.isEmpty(userid)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			} else {
				userService.deleteuser(userid);
				resultMessage.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}

	/**
	 * 返回事件历史
	 */
	@RequestMapping(value = "/listhistory", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取单个事件历史记录")
	private ResultMessage historyjsp(String thingid) {
		ResultMessage resultMessage = new ResultMessage();
		Map<String, Object> map = new HashMap<>();
		try {
			if (StringUtil.isEmpty(thingid)) {
				resultMessage.setSuccess(false);
				resultMessage.setMessage("参数为空");
			} else {
				Map<String, Object> map2 = thingService.getrecordlisthistory(thingid);
				map.put("thing", map2.get("thing"));
				map.put("list", map2.get("list"));
				resultMessage.setData(map);
				resultMessage.setSuccess(true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMessage(e.getMessage());
			resultMessage.setSuccess(false);
		}
		return resultMessage;
	}
	
	@RequestMapping(value = "countselect", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "统计分类查询（反查）",notes="时间为空默认所有，单位参数如果为空则默认总计")
	public resultlist countselectlist(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam Integer typeid,String startDate,String endDate,String unitida,@ApiParam("区分表头列的参数classify=1总计，2办理中，3已经办结,4满意，5基本满意，6不满意,7未评价")@RequestParam Integer classify) {
		resultlist resultMessage = new resultlist();
		try {
			if (page <= 0 || limit <= 0) {
				resultMessage.setMsg("参数错误");
				resultMessage.setCode(1);
			} else {
				PageHelper.startPage(page, limit);
				List<thing> things = thingService.countselectlist(typeid,startDate,endDate,unitida,classify);
				PageInfo<thing> thinginfor = new PageInfo<>(things);

				resultMessage.setCount(thinginfor.getTotal());
				resultMessage.setData(thinginfor.getList());
				resultMessage.setCode(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMessage.setMsg(e.getMessage());
			resultMessage.setCode(1);
		}
		return resultMessage;
	}
	
}
